package com.xayoudao.uneng.domain;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.json.JSONArray;
import org.json.JSONObject;

import com.xayoudao.uneng.util.DeviceUtils;

 

/**
 * 某用户的、某项活动的奖券的bean
 *
 * @author fengxue
 * 
 */
public class Company {

	private int id;               //奖券id
	private int userid = 0;       //用户ID
	private int eventid = 0;      //活动ID
	private int code = 0;         //奖券号码
	private int winning = 0;      //是否中奖
	private int stat = 0;         //是否已经兑换过奖品，0是没有兑换，1是兑换过
	private String createtime;    //摇奖时间

	
	private List<Company> ticketsInfos = new ArrayList<Company>();

	public List<Company> getTicketsInfos() {
		return ticketsInfos;
	}

	public Company() {

	}

	public Company(int userid, int eventid ) {
		super();
		this.userid = userid;
		this.eventid = eventid;
		this.code = showRandomInteger(0,999);
	}
	
	public Company(int userid, int eventid, int code ,int winning) {
		super();
		this.userid = userid;
		this.eventid = eventid;
		this.code = code;
		this.winning = winning ;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getEventid() {
		return eventid;
	}

	public void setEventid(int eventid) {
		this.eventid = eventid;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public int getWinning() {
		return winning;
	}

	public void setWinning(int winning) {
		this.winning = winning;
	}

	public String getCreatetime() {
		return createtime;
	}

	public void setCreatetime(String createtime) {
		this.createtime = createtime;
	}

	@Override
	public String toString() {
		// return "TicketsInfo [id=" + id + ", eventid=" + eventid + ", code="
		// + code + ", winning=" + winning + ", createtime=" + createtime
		// + "]";

		return String.format("%03d", userid) + " "
				+ String.format("%03d", eventid) + " "
				+ String.format("%03d", code);
	}
	//解析数据到TicketsInfo集合
	public void parser(JSONObject jsonObject) {
		JSONArray jsonArray = jsonObject.optJSONArray("data");

		for (int i = 0; i < jsonArray.length(); i++) {
			Company ticketsInfo = new Company();

			ticketsInfo.setId(jsonArray.optJSONObject(i).optInt("id"));
			ticketsInfo.setStat(jsonArray.optJSONObject(i).optInt("stat"));
			ticketsInfo.setUserid(jsonArray.optJSONObject(i).optInt("userid"));
			ticketsInfo
					.setEventid(jsonArray.optJSONObject(i).optInt("eventid"));
			ticketsInfo.setCode(jsonArray.optJSONObject(i).optInt("code"));
			ticketsInfo
					.setWinning(jsonArray.optJSONObject(i).optInt("winning"));
			ticketsInfo.setCreatetime(jsonArray.optJSONObject(i).optString(
					"createtime"));

			ticketsInfos.add(ticketsInfo);

		}
	}

	public int getUserid() {
		return userid;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	public String toMD5(){
		String md5 = DeviceUtils.toMD5(
				userid+""+eventid+""+code+ ((""+code).length())) ;
		return md5;
	}
	
	public int getStat() {
		return stat;
	}

	public void setStat(int stat) {
		this.stat = stat;
	}
	
	/**
	 * 取得三位随机数的方法
	 * @param aStart
	 * @param aEnd
	 * @param aRandom
	 * @return
	 */
	private  int showRandomInteger(int aStart, int aEnd) {
		Random aRandom = new Random();
		if (aStart > aEnd) {
			throw new IllegalArgumentException("Start cannot exceed End.");
		}
		// get the range, casting to long to avoid overflow problems
		long range = (long) aEnd - (long) aStart + 1;
		// compute a fraction of the range, 0 <= frac < range
		long fraction = (long) (range * aRandom.nextDouble());
		int randomNumber = (int) (fraction + aStart);
		
		return  randomNumber;
	}
}
